package com.yg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.pojo.TBook;
import com.yg.mapper.TBookMapper;
import com.yg.pojo.vo.PageVo;
import com.yg.service.TBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yg.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kotori
 * @since 2022-11-18
 */
@Service
public class TBookServiceImpl extends ServiceImpl<TBookMapper, TBook> implements TBookService {

    @Autowired
    private TBookMapper bookMapper;
    @Autowired
    private PageUtil<TBook> pageUtil;

    @Override
    public PageVo<TBook> allBookPage(Long pageNo, Long pageSize, String url) {
        if (pageNo == 0) {
            pageNo = 1L;
        }
        if (pageSize == 0) {
            pageSize = 4L;
        }
        Page<TBook> page = new Page<>(pageNo, pageSize);
        bookMapper.selectPage(page, null);
        return pageUtil.Page2PageVo(page, url);
    }

    @Override
    public PageVo<TBook> bookByPricePage(Integer max, Integer min, Long pageNo, Long pageSize, String url) {
        if (pageNo == 0) {
            pageNo = 1L;
        }
        if (pageSize == 0) {
            pageSize = 4L;
        }
        LambdaQueryWrapper<TBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(TBook::getPrice, min).le(TBook::getPrice, max);
        Page<TBook> page = new Page<>(pageNo, pageSize);
        bookMapper.selectPage(page, lambdaQueryWrapper);
        return pageUtil.Page2PageVo(page, url);
    }
}
